package test2.Operation;

import test2.Student;

import java.util.Scanner;
import java.util.TreeMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ling
 * Date: 2022-04-22
 * Time: 21:58
 */
public class AddOperation implements  IOperation{
    @Override
    public void work(TreeMap<String, Student> treeMap) {
        System.out.println("数据录入");
        Scanner scanner=new Scanner(System.in);
        String id=null;
        String name=null;
        String tel=null;
        String email=null;
        int age=0;
        while(true){
            System.out.println("请输入学号，学号为10个数字");
            id=scanner.nextLine();
            if(checkId(id)){
                System.out.println("输入成功");
            }else{
                System.out.println("输入格式有误,请全部重新输入");
                continue;
            }
            System.out.println("请输入姓名");
            name=scanner.nextLine();
            if(checkName(name)){
                System.out.println("输入成功");
            }else{
                System.out.println("输入格式有误,请全部重新输入");
                continue;
            }

            System.out.println("请输入年龄");
            age=scanner.nextInt();
            if(checkAge(age)){
                System.out.println("输入成功");
            }else{
                System.out.println("输入格式有误,请全部重新输入");
                continue;
            }
            System.out.println("请输入手机号");
            Scanner scanner1=new Scanner(System.in);
            tel=scanner1.nextLine();
            if(checkTel(tel)){
                System.out.println("输入成功");
            }else{
                System.out.println("输入格式有误,请全部重新输入");
                continue;
            }
            Scanner scanner2=new Scanner(System.in);
            System.out.println("请输入邮箱");
             email=scanner2.nextLine();
            if(checkEmail(email)){
                System.out.println("输入成功");
            }else{
                System.out.println("输入格式有误，重新输入");
                continue;
            }
            break;
        }

    treeMap.put(id,new Student(id,name,tel,email,age));

    }
    public boolean checkId(String str){
        return str.matches("^[0-9]{10}$");
    }
    public boolean checkName(String str){
        return str.matches("^[\u4E00-\u9FA5]{2,4}$");
    }
    public boolean checkAge(int age){
        if(age>=5&&age<=100)
            return true;
        else
            return false;
    }
    public boolean checkTel(String str){
        return str.matches("^[0-9]{11}$");
    }
    public static boolean checkEmail(String str){
        return str.matches("^([a-zA-Z][a-zA-Z0-9_.]{4,19})@([0-9a-zA-Z_.]{1,10})\\.(com.cn|com.cn.net)$");
    }
}
